package org.meeting.radish.service.impl;

import org.meeting.radish.entity.UserAnswer;
import org.meeting.radish.mapper.UserAnswerMapper;
import org.meeting.radish.service.UserAnswerService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class UserAnswerServiceImpl implements UserAnswerService {

    @Resource
    UserAnswerMapper userAnswerMapper;

    @Override
    public Integer submitUserAnswer(UserAnswer userAnswer) {
        UserAnswer answer = userAnswerMapper.queryUserAnswer(userAnswer);
        userAnswer.setDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        if (answer == null) {
            return userAnswerMapper.addUserAnswer(userAnswer);
        }
        return userAnswerMapper.updateUserAnswer(userAnswer);
    }

    @Override
    public List<UserAnswer> queryUserAnswerByUid(Integer uid) {
        return userAnswerMapper.queryUserAnswerByUid(uid);
    }

}
